package com.iflytek.threadpool.manager.service.impl;

import com.iflytek.threadpool.manager.service.DemoService;
import com.iflytek.threadpool.manager.service.PrintService;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: threadpooltest
 * @description:
 * @author: 陈亮
 * @create: 2022-02-22 20:55
 */
@Service
public class DemoServiceImpl implements DemoService {


    @Resource
    private TaskExecutor taskExecutor;

    @Resource
    private PrintService printService;

    @Override
    public String getResult() {
        // 方法一 自己executor 不解耦 用一次写一次
        //method01();
        // 方法二 构建异步service
        method02();
        System.out.println(Thread.currentThread().getName() + "准备return");
        return "succeed";
    }

    private void method02() {
        printService.printMessage02();
        printService.printMessage01();
    }

    private void method01() {
        taskExecutor.execute(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("这是" + Thread.currentThread().getName() + "在" + i + "次打印");
            }
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
